<?php 
App::uses('AppController', 'Controller');

class PostingFreeController extends AppController {
	
	var $components = array('Link','Files','Images','Strings');
	/**
	 * @author: QuocTran
	 */		
	public function index() {
		$userID = 0;
		if ($this->Auth->loggedIn()) {
			$user = $this->Session->read('Auth.User');
			if($user['group_id'] == GROUP_MEMBER){
				$userID = $user['id'];
			}
		}
		
		$this->loadModel('RootCategory');
		$this->loadModel('Region');
		$this->loadModel('Post');
		$this->loadModel('Image');
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		$this->set('regions',$this -> Region->findAllData());
		$this->set('title_for_layout','Đăng tin miễn phí');
		
		$this->layout = 'layoutNoMenuLeft';
		
		if ($this -> request -> is('post')) {
			date_default_timezone_set('Asia/Ho_Chi_Minh');
			$new = $this -> data;
			$new['title_vn'] = $this->Strings->replaceHTMLTag($new['title_vn']);
			$new['short_url'] = $this->Link->getShortLink($new['title_vn']);
			$new['title_en'] = $this->Link->vietnamese_permalink($new['title_vn'], " ");
			$new['image_icon'] = $this->Images->createIconImage($new['img'], ICON_WIDTH, ICON_HEIGHT);
			$new['price'] = str_replace(",", "", $new['price']);
			$new['product_country'] = $this->Strings->replaceHTMLTag($new['product_country']);
			$new['content'] = $this->Strings->replaceHTMLTag($new['content']);
			$new['sell_name'] = $this->Strings->replaceHTMLTag($new['sell_name']);
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['created_by'] = $userID;
			$new['updated_by'] = $userID;
			$new['user_id'] = $userID;
			$new['updated_at'] = date("Y-m-d H:i:s");
			
			if ($this -> Post -> save($new)){
				$idPost = $this->Post->id;
				$imgList = $new['img'];
				$imgNew= array();
				foreach ($imgList as $i) {
					if(!empty($i)){
						$img['url']=$i;
						$img['post_id']=$idPost;
						$img['deleted'] = 0;
						$img['created_by'] = -1;
						$img['created_at'] = date("Y-m-d H:i:s"); 
						$img['updated_by'] = -1;
						$img['updated_at'] = date("Y-m-d H:i:s"); 
						array_push($imgNew, $img);
					}
				}
				if(count($imgNew)>0){
					$this->Image->saveMany($imgNew);
				}
				$this->redirect('/');
				
			}
		}
	}
	
	/**
	 * QuocTran
	 * ajax upload image
	 */
	public function uploadImage(){
		if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_FILES['image']) ) {
			echo json_encode(array('success'=>'2','path'=>"Invalid file!"));
			exit();
		}
		if ($this->Files ->isValidFileType($_FILES['image']['name'])){
			//upload file
			$path = $this->Files->uploadFile(UPLOAD_IMG_POST, $_FILES['image']['tmp_name'], ROOT_IMG_POST, $_FILES['image']['name']);
			//resize
			//$this->Image->resizeImage(WWW_ROOT.$path, IMAGE_WIDTH, IMAGE_HEIGHT);
			echo json_encode(array('success'=>'1','path'=>$path));
		}else{
			echo json_encode(array('success'=>'2','path'=>""));
		}
		exit();
	}
	/**
	 * QuocTran
	 * delete image
	 */
	public function deleteImage(){
		if ($this -> request -> is('post')) {
			$path = WWW_ROOT.substr($_POST['src'],1);
			$this->Files -> deleteFile($path);
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('success'=>false));
		}
		exit();
	}
	/**
	 * @author: QuocTran
	 */		
	public function edit($id) {
		$userID = 0;
		if ($this->Auth->loggedIn()) {
			$user = $this->Session->read('Auth.User');
			if($user['group_id'] == GROUP_MEMBER){
				$userID = $user['id'];
			}
		}else{
			$this->redirect('/');
		}
		
		$this->set("action","/PostingFree/edit");
		$this->loadModel('RootCategory');
		$this->loadModel('Region');
		$this->loadModel('Post');
		$this->loadModel('Image');
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		$this->set('regions',$this -> Region->findAllData());
		$this->set('title_for_layout','Chỉnh sửa tin');
		
		$this->layout = 'layoutNoMenuLeft';
		$data = $this->Post->findById($id);
		$this->set("data",$data);
		$this->set("img",$this->Image->findAllByPostId($id));
		if ($this -> request -> is('post')) {
			$update = $this -> data;
			$update['id'] = $id;
			$update['title_vn'] = $this->Strings->replaceHTMLTag($update['title_vn']);
			$update['title_en'] = $this->Link->vietnamese_permalink($update['title_vn'], " ");
			$update['price'] = str_replace(",", "", $update['price']);
			$update['product_country'] = $this->Strings->replaceHTMLTag($update['product_country']);
			$update['content'] = $this->Strings->replaceHTMLTag($update['content']);
			$update['sell_name'] = $this->Strings->replaceHTMLTag($update['sell_name']);
			$update['updated_at'] = date("Y-m-d H:i:s");
			if ($this -> Post -> save($update)){
				$postID= $id;
				$imgList = $update['img'];
				$imgNew= array();
				foreach ($imgList as $i) {
					$img = array();
					if(!empty($i['id']) || !empty($i['url']) ){
						
						$img['post_id'] = $postID;
						$img['deleted'] = 0;
						$img['created_by'] = $userID;//lấy id của user login
						$img['created_at'] = date("Y-m-d H:i:s");
						$img['updated_by'] = $userID;//lấy id của user login
						$img['updated_at'] = date("Y-m-d H:i:s");
						if(isset($i['id'])){
							$img['id']=$i['id'];
						}
						if(!empty($i['url'])){
							$img['url']=$i['url'];
						}
						if(!empty($i['id']) && empty($i['url'])){
							$img['deleted'] = 1;
						}
							
						array_push($imgNew, $img);
					}
				}
				if(count($imgNew)>0){
					
					$this->Image->saveMany($imgNew);
				}
				$this->redirect('/quan-ly-user/tin-dang/');
			}
		}
	}
		
}
?>